package p270jp.pxv.android.view;

import android.content.Context;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import com.google.android.gms.actions.SearchIntents;
import java.util.Objects;
import p014ae.View$OnClickListenerC0054c;
import p079cl.SearchQueryEditorPresenter;
import p079cl.SearchResultPresenter;
import p090d0.C2518n;
import p109dj.OnTextChangedListener;
import p134ei.PixivAnalytics;
import p184gi.ABTestingEvent;
import p209hg.SearchQueryEditorContract;
import p270jp.pxv.android.R;
import p270jp.pxv.android.activity.SearchResultActivity;
import p270jp.pxv.android.view.SearchQueryEditorView;
import p270jp.pxv.android.view.SearchWordView;
import p314lg.ViewSearchQueryEditorBinding;
import p325m0.MutabilityOwnership;
import p344mj.SearchQueryEditorActionListener;

/* renamed from: jp.pxv.android.view.SearchQueryEditorView */
/* loaded from: classes2.dex */
public class SearchQueryEditorView extends LinearLayout implements SearchQueryEditorContract {

    /* renamed from: a */
    public SearchQueryEditorPresenter f19674a = new SearchQueryEditorPresenter(this, (PixivAnalytics) C2518n.m7641d(PixivAnalytics.class));

    /* renamed from: b */
    public boolean f19675b;

    /* renamed from: c */
    public ViewSearchQueryEditorBinding f19676c;

    /* renamed from: jp.pxv.android.view.SearchQueryEditorView$a */
    /* loaded from: classes2.dex */
    public class C4390a extends OnTextChangedListener {
        public C4390a() {
            SearchQueryEditorView.this = r1;
        }

        @Override // p109dj.OnTextChangedListener, android.text.TextWatcher
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            SearchQueryEditorView searchQueryEditorView = SearchQueryEditorView.this;
            if (searchQueryEditorView.f19675b) {
                searchQueryEditorView.f19675b = false;
                return;
            }
            SearchQueryEditorPresenter jVar = searchQueryEditorView.f19674a;
            String charSequence2 = charSequence.toString();
            SearchQueryEditorActionListener bVar = jVar.f5568b;
            if (bVar != null) {
                SearchResultPresenter kVar = (SearchResultPresenter) ((SearchResultActivity) bVar).f18666b0;
                Objects.requireNonNull(kVar);
                MutabilityOwnership.m3745m(charSequence2, SearchIntents.EXTRA_QUERY);
                kVar.f5580j = charSequence2;
                kVar.m9712n(charSequence2);
            }
        }
    }

    /* renamed from: jp.pxv.android.view.SearchQueryEditorView$b */
    /* loaded from: classes2.dex */
    public class C4391b implements SearchWordView.SearchWordViewListener {

        /* renamed from: a */
        public final /* synthetic */ int f19678a;

        public C4391b(int i, String str) {
            SearchQueryEditorView.this = r1;
            this.f19678a = i;
        }

        @Override // p270jp.pxv.android.view.SearchWordView.SearchWordViewListener
        public void onClickSearchWordContainer() {
            SearchQueryEditorPresenter jVar = SearchQueryEditorView.this.f19674a;
            jVar.f5569c = SearchQueryEditorPresenter.m9720i(jVar.f5569c);
            ((SearchQueryEditorView) jVar.f5567a).m4470a();
            ((SearchQueryEditorView) jVar.f5567a).m4468c(jVar.f5569c);
            SearchQueryEditorActionListener bVar = jVar.f5568b;
            if (bVar != null) {
                ((SearchResultActivity) bVar).m4704k1(jVar.f5569c);
            }
        }

        @Override // p270jp.pxv.android.view.SearchWordView.SearchWordViewListener
        public void onClickSearchWordDeleteImageView() {
            SearchQueryEditorPresenter jVar = SearchQueryEditorView.this.f19674a;
            int i = this.f19678a;
            String[] k = SearchQueryEditorPresenter.m9718k(jVar.f5569c);
            StringBuilder sb = new StringBuilder();
            for (int i2 = 0; i2 < k.length; i2++) {
                if (i2 != i) {
                    sb.append(k[i2]);
                    sb.append(" ");
                }
            }
            String trim = sb.toString().trim();
            jVar.f5569c = trim;
            if (trim.isEmpty()) {
                ((SearchQueryEditorView) jVar.f5567a).m4470a();
                ((SearchQueryEditorView) jVar.f5567a).m4468c(jVar.f5569c);
                SearchQueryEditorActionListener bVar = jVar.f5568b;
                if (bVar != null) {
                    ((SearchResultActivity) bVar).m4704k1(jVar.f5569c);
                    return;
                }
                return;
            }
            ((SearchQueryEditorView) jVar.f5567a).m4469b(SearchQueryEditorPresenter.m9718k(jVar.f5569c));
            SearchQueryEditorActionListener bVar2 = jVar.f5568b;
            if (bVar2 != null) {
                ((SearchResultActivity) bVar2).m4709f1(jVar.f5569c);
            }
        }
    }

    public SearchQueryEditorView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        ViewSearchQueryEditorBinding hbVar = (ViewSearchQueryEditorBinding) DataBindingUtil.m11370c(LayoutInflater.from(getContext()), R.layout.view_search_query_editor, this, true);
        this.f19676c = hbVar;
        hbVar.f21665r.addTextChangedListener(new C4390a());
        this.f19676c.f21665r.setOnEditorActionListener(new TextView.OnEditorActionListener() { // from class: nm.n0
            @Override // android.widget.TextView.OnEditorActionListener
            public final boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                SearchQueryEditorView searchQueryEditorView = SearchQueryEditorView.this;
                SearchQueryEditorPresenter jVar = searchQueryEditorView.f19674a;
                String obj = searchQueryEditorView.f19676c.f21665r.getText().toString();
                Objects.requireNonNull(jVar);
                if (i != 3) {
                    return false;
                }
                String j = SearchQueryEditorPresenter.m9719j(obj);
                if (!TextUtils.isEmpty(j)) {
                    jVar.f5569c = j;
                    SearchQueryEditorView searchQueryEditorView2 = (SearchQueryEditorView) jVar.f5567a;
                    searchQueryEditorView2.f19676c.f21665r.clearFocus();
                    searchQueryEditorView2.f19676c.f21665r.setVisibility(8);
                    ((SearchQueryEditorView) jVar.f5567a).m4469b(SearchQueryEditorPresenter.m9718k(jVar.f5569c));
                    if (jVar.f5568b != null) {
                        jVar.f5570d.m6526d(ABTestingEvent.C3330b.f15548a);
                        jVar.f5570d.m6526d(ABTestingEvent.C3329a.f15547a);
                        ((SearchResultActivity) jVar.f5568b).m4709f1(jVar.f5569c);
                    }
                }
                return true;
            }
        });
        this.f19676c.f21665r.setOnFocusChangeListener(new View.OnFocusChangeListener() { // from class: nm.m0
            @Override // android.view.View.OnFocusChangeListener
            public final void onFocusChange(View view, boolean z) {
                SearchQueryEditorView searchQueryEditorView = SearchQueryEditorView.this;
                Objects.requireNonNull(searchQueryEditorView);
                if (z) {
                    ((InputMethodManager) searchQueryEditorView.getContext().getSystemService("input_method")).showSoftInput(searchQueryEditorView.f19676c.f21665r, 0);
                }
            }
        });
        this.f19676c.f21666s.setOnClickListener(new View$OnClickListenerC0054c(this, 29));
    }

    /* renamed from: a */
    public void m4470a() {
        this.f19676c.f21664q.setVisibility(8);
        this.f19676c.f21664q.removeAllViews();
    }

    /* renamed from: b */
    public void m4469b(String[] strArr) {
        this.f19676c.f21664q.setVisibility(0);
        this.f19676c.f21664q.removeAllViews();
        for (int i = 0; i < strArr.length; i++) {
            String str = strArr[i];
            SearchWordView searchWordView = new SearchWordView(getContext());
            searchWordView.setSearchWord(str);
            searchWordView.setSearchWordViewListener(new C4391b(i, str));
            this.f19676c.f21664q.addView(searchWordView);
        }
        Space space = new Space(getContext());
        space.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.search_query_editor_view_left_padding), -2));
        this.f19676c.f21664q.addView(space);
    }

    /* renamed from: c */
    public void m4468c(String str) {
        this.f19676c.f21665r.setVisibility(0);
        this.f19676c.f21665r.setText(str);
        this.f19676c.f21665r.requestFocus();
        this.f19676c.f21665r.setSelection(str.length());
    }

    public String getSearchQuery() {
        SearchQueryEditorPresenter jVar = this.f19674a;
        String obj = this.f19676c.f21665r.getText().toString();
        Objects.requireNonNull(jVar);
        return SearchQueryEditorPresenter.m9719j(obj);
    }

    @Override // android.view.View
    public void onRestoreInstanceState(Parcelable parcelable) {
        super.onRestoreInstanceState(parcelable);
        this.f19675b = true;
    }

    public void setSearchQuery(String str) {
        SearchQueryEditorPresenter jVar = this.f19674a;
        Objects.requireNonNull(jVar);
        jVar.f5569c = SearchQueryEditorPresenter.m9719j(str);
        SearchQueryEditorView searchQueryEditorView = (SearchQueryEditorView) jVar.f5567a;
        searchQueryEditorView.f19676c.f21665r.clearFocus();
        searchQueryEditorView.f19676c.f21665r.setVisibility(8);
        ((SearchQueryEditorView) jVar.f5567a).m4469b(SearchQueryEditorPresenter.m9718k(jVar.f5569c));
    }

    public void setSearchQueryEditorActionListener(SearchQueryEditorActionListener bVar) {
        this.f19674a.f5568b = bVar;
    }
}
